package com.tlgen.modules.vocation.controller;

import com.tlgen.common.exception.BadRequestException;
import com.tlgen.modules.system.service.dto.RoleDto;
import com.tlgen.modules.vocation.domain.Family;
import com.tlgen.modules.vocation.service.FamilyService;
import com.tlgen.modules.vocation.service.dto.FamilyDto;
import com.tlgen.modules.vocation.service.dto.FamilyQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

@Api(tags = "系统：参合家庭信息管理")
@RestController
@RequestMapping("/api/family")
public class FamilyController {

    private final FamilyService familyService;

    private static final String ENTITY_NAME = "family";

    public FamilyController(FamilyService familyService) {
        this.familyService = familyService;
    }

    @ApiOperation("返回全部的家庭档案信息")
    @GetMapping(value = "/all")
    @PreAuthorize("@tlgen.check('family:list')")
    public ResponseEntity<Object> getAll(@PageableDefault(value = 2000, sort = {"id"}, direction = Sort.Direction.ASC) Pageable pageable){
        return new ResponseEntity<>(familyService.queryAll(pageable), HttpStatus.OK);
    }

    @ApiOperation("查询")
    @GetMapping
    @PreAuthorize("@tlgen.check('family:list')")
    public ResponseEntity<Object> getfamilys(FamilyQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(familyService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @ApiOperation("新增")
    @PostMapping
    @PreAuthorize("@tlgen.check('family:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Family resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        return new ResponseEntity<>(familyService.create(resources), HttpStatus.CREATED);
    }

    @ApiOperation("修改")
    @PutMapping
    @PreAuthorize("@tlgen.check('family:edit')")
    public ResponseEntity<Object> update(@Validated(Family.Update.class) @RequestBody Family resources){
        familyService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiOperation("删除")
    @DeleteMapping
    @PreAuthorize("@tlgen.check('family:del')")
    public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){
        familyService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@tlgen.check('family:list')")
    public void download(HttpServletResponse response, FamilyQueryCriteria criteria) throws IOException {
        familyService.download(familyService.queryAll(criteria), response);
    }

    @ApiOperation("修改户主对应的家庭成员")
    @PutMapping(value = "/home")
    @PreAuthorize("@tlgen.check('families:edit')")
    public ResponseEntity<Object> updateHome(@RequestBody Family resources){
        FamilyDto family = familyService.findById(resources.getId());
        familyService.updateHome(resources,family);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


}
